<?php

namespace app\api\service;

use app\api\model\AccountType;
use app\api\model\SetAccount;
use think\Db;

class SetAccountTransfer
{
    use SetAccountFlow;

    /**
     *  新增账户互转
     * @param array $data
     * @return bool|\think\Collection
     */
    public static function mutualTransfer(array $data)
    {
        return Db::transaction(function () use ($data) {
            $res[] = self::inTransfer($data);
            $outData = $data;
            $outData['set_account_id'] = $data['out_account_id'];
            $res[] = self::outTransfer($outData);
            if (empty(array_filter($res))) {
                return false;
            } else {
                return true;
            }
        });
    }


    /**
     * 收入记账
     * @param array $data
     * @return bool
     */
    public static function inTransfer(array $data)
    {
        return Db::transaction(function () use ($data) {
            $data['customer_type'] = 'set_account';
            $data['customer_id'] = $data['set_account_id'];
            $data['account_type_id'] = AccountType::where('name', '账户互转')->find()['id'];
            $res[] =  self::newFlow(array_merge([
                'business_type' => '收入记账',
                'business_number' => 'jxc' . time(),
                'in_amount' => $data['amount']
            ], $data));
            $setAccount = SetAccount::find($data['set_account_id']);
            $setAccount->money = $setAccount->money + $data['amount'];
            $res[] = $setAccount->save();
            if (empty(array_filter($res))) {
                return false;
            } else {
                return true;
            }
        });
    }

    /**
     * 支出记账
     * @param $data
     * @return bool
     */
    public static function outTransfer($data)
    {
        return Db::transaction(function () use ($data) {
            $data['customer_type'] = 'set_account';
            $data['customer_id'] = $data['set_account_id'];
            $data['account_type_id'] = AccountType::where('name', '账户互转')->find()['id'];
            $res[] =  self::newFlow(array_merge([
                'business_type' => '支出记账',
                'business_number' => 'jxc' . time(),
                'out_amount' => $data['amount']
            ], $data));
            $setAccount = SetAccount::find($data['set_account_id']);
            $setAccount->money = $setAccount->money - $data['amount'];
            $res[] = $setAccount->save();
            if (empty(array_filter($res))) {
                return false;
            } else {
                return true;
            }
        });
    }

    /**
     * 期初调整
     * @param $data
     * @return mixed
     */
    public static function adjustment($data)
    {
        return Db::transaction(function () use ($data) {
            $data['customer_type'] = 'set_account';
            $data['customer_id'] = $data['set_account_id'];
            $res[] =  self::newFlow(array_merge([
                'account_type_id' => AccountType::where('name', '账户互转')->find()['id'],
                'business_type' => '期初调整',
                'business_number' => 'jxc' . time(),
                'in_amount' => $data['amount']
            ], $data));
            $setAccount = SetAccount::find($data['set_account_id']);
            $beginning = $data['amount'];
            $oldBeginning = $setAccount['beginning'];
            $oldAmount = $setAccount['money'];
            if ($oldBeginning) {
                //  更新时 余额为 原金额-原期初金额+期初金额
                $amount = ($oldAmount - $oldBeginning) + $beginning;
                $res[] = $setAccount->save(['money' => $amount, 'beginning' => $beginning]);
            } else {
                // 新增时 余额为 原金额+期初金额
                $amount = $oldAmount + $beginning;
                $res[] = $setAccount->save(['money' => $amount, 'beginning' => $beginning]);
            }
//            $setAccount->money = $data['amount'];
//            $res[] = $setAccount->save();
            if (empty(array_filter($res))) {
                return false;
            } else {
                return true;
            }
        });
    }
}
